#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 14 11:34:42 2018
@project: 天池比赛-A股主板上市公司公告信息抽取
@group: MZH_314
@author: LHQ

抽取表格数据后对数据初次整理
"""
import pandas as pd


class Table:
    """
    对从公告html里提取出来的表格做进一步的处理
    """
    def __init__(self, desc, rows: list):
        """
        Args:
            desc: 表格的一些描述文本
            rows: 表格数据的列表，列表中每个元素是表格行数据
        """
        self.desc = desc
        self.rows = [[],[]] if len(rows) < 2 else rows 
        self.index = self.seperate_index(rows)
        
    def seperate_index(self, rows):
        """field和data的分隔位置
        """
        col_0 = [row[0] for row in rows if row]   # 第一列 
        index = 0 
        pre_v = None
        for i, v in enumerate(col_0):
            if i == 0:
                pre_v = v
                continue
            if v == pre_v:
                index = i
            else:
                break
        return index

    @property
    def fields(self):
        """表格的字段名称,主要是处理合并单元格的情况
        """
        if self.index == 0:
            fields = self.rows[0]
        else:
            fields = []
            n = len(self.rows[0])
            for i in range(n):
                tmp = []
                for j in range(self.index+1):
                    v = str(self.rows[j][i])
                    if v not in tmp:
                        tmp.append(v)
                fields.append("_".join(tmp))
        return fields
            
    @property
    def data(self):
        return [row for row in self.rows[self.index+1:]]

    def to_df(self):
        df = pd.DataFrame(self.data, columns=self.fields)
        return df


class ZjcTable(Table):
    """
    增减持表格处理
    """
    def to_df(self):
        df = super().to_df()
        if df.empty:
            return df
        # 对减持后的合计持有股份进行过滤
        columns = df.columns
        if "股份性质" in columns:
            col_indexes = [i for i, v in enumerate(columns) if v == "股份性质"]
            ind = set()
            for col_i in col_indexes:
                values = df.iloc[:, col_i].tolist() 
                ind.update([ i for i, v in enumerate(values) if "合计持有股份" in v])
            if len(ind) > 0:
                df = df.iloc[list(ind), :]
        # 去掉含有‘合计’的行
        hj = df.isin(['合计', '合计：']).any(axis=1)
        df = df[~hj]
        # 去掉含有‘累计’的行
        col_0 = df.iloc[:, 0].tolist()
        ind = [i for i, v in enumerate(col_0) if '累计' not in v]
        df = df.iloc[ind, :]
        # 第一列数值为空则向前填充
#        df = df.replace({field_0: ''}, {field_0: None}, method='ffill')
        df.iloc[:,0] = df.iloc[:,0].replace("", method='ffill')
        return df   

